package model;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * <b>Description</b>
 * </br> Repair is a representation of a real-life repair being conducted on a machine.
 * </br> <b>Example:</b><br>
 * <code>  Repair repair = new Repair(Machine machine); </code>
 * @author Bogdan
 * @param Machine machine = the machine being repaired 
 * </br>
	
 * @since this morning
 */
public class Repair {

	private GregorianCalendar startDate = null;
	private GregorianCalendar endDate = null;
	private Machine machine = null;
	private List<RepairLine> repairLines = new ArrayList<RepairLine>(); //requires Usage class
//	private final boolean machineState = true;

	/**
	 * Requires machine != null
	 * @param machine
	 */
	public Repair(Machine machine) {
		this.machine = machine;
		startDate = new GregorianCalendar();
		machine.addRepair(this);
		machine.setState(false);
	}

	public GregorianCalendar getStartDate() {
		return startDate;
	}

	public void setStartDate(GregorianCalendar startDate) {
		this.startDate = startDate;
	}

	public Machine getMachine() {
		return machine;
	}

	public void setMachine(Machine machine) {
		if (this.machine != machine) {
			if (this.machine != null) {
				this.machine.removeRepair(this);
				this.machine = machine;
				if (machine != null) {
					machine.addRepair(this);
				}
			}
		}
	}

	public GregorianCalendar getEndDate() {
		return endDate;
	}

	public List<RepairLine> getRepairLines() {
		return repairLines;
	}

	public void addRepairLine(RepairLine e) {
		if (!repairLines.contains(e))
			repairLines.add(e);
	}

	public void removeRepairLine(RepairLine e) {
		if (repairLines.contains(e))
			repairLines.remove(e);
	}

	public void createRepairLine(SparePart part, int amount) {
		repairLines.add(new RepairLine(part, amount));
	}

	public void endRepair() {
		this.machine.setState(true);
		endDate = new GregorianCalendar();
		
		//calculate enddate - startdate;
	}

	/**
	 * Return the total Downtime of the repair of the current machine .
	 * @author Nikolay
	 */
	public int calcDowntime() {
		return (int) ((endDate.getTimeInMillis() - startDate.getTimeInMillis())
				/ (60 * 60 * 1000) + 1);
	}

	//-----------------------------------------------------------------------
	public void updateRepair(Machine machine) {
		this.machine = machine;
	}

	public void updateRepairLine(SparePart part, int amount,
			RepairLine repairLine) {
		repairLine.updateRepairLine(part, amount);
	}

	//----------------------------------------------------------------------

//	public void changeMachineState() {
//		if (endDate == null) {
//			this.getMachine().setState(false);
//			System.out.println("OFFLINE");
//		} else if (endDate != null) {
//			this.getMachine().setState(true);
//			System.out.println("ONLINE");
//		}
//
//	}

	public String getDate(GregorianCalendar d){
		if(d != null){
		String month = d.get(GregorianCalendar.DATE) + "";
		String day = d.get(GregorianCalendar.MONTH ) + "";
		String year = d.get(GregorianCalendar.YEAR) + "";
		
		if (d.get(GregorianCalendar.DATE) < 10)  month = "0" + d.get(GregorianCalendar.DATE);
		if ((d.get(GregorianCalendar.MONTH) + 1) < 10) day = "0" + (d.get(GregorianCalendar.MONTH) + 1);
		
		return day + "-" + month + "-" + year;
		}
		else return "N/A";
	}
	
	/**
	 * @author George
	 */
	public String toString() {
//		String month = startDate.get(GregorianCalendar.DATE) + "";
//		String day = startDate.get(GregorianCalendar.MONTH ) + "";
//		
//		if (startDate.get(GregorianCalendar.DATE) < 10)  month = "0" + startDate.get(GregorianCalendar.DATE);
//		if ((startDate.get(GregorianCalendar.MONTH) + 1) < 10) day = "0" + (startDate.get(GregorianCalendar.MONTH) + 1);
//		
//		return day + "-" + month + "-"
//				+ startDate.get(GregorianCalendar.YEAR) + "";
		return getDate(startDate);

	}

	//-----------------------------------------------------------------------
	/**
	 * The RepairLine class contain the amount of the spare part and the specific
	 * spare part type. <br>
	 * @author Nikolay
	 */
	public class RepairLine {

		private int amount;
		private SparePart part;

		/**
		 * <code>  RepairLine repairLine = new RepairLine(part,amount); </code>
		 * 
		 * @author Nikolay
		 * @param SparePart
		 *            part = The specific spare part type . </br> int amount =The
		 *            amount of the spare part.
		 */
		private RepairLine(SparePart part, int amount) {
			this.amount = amount;
			this.part = part;
		}

		public int getAmount() {
			return amount;
		}

		public void setAmount(int amount) {
			this.amount = amount;
		}

		public SparePart getPart() {
			return part;
		}

		public void setSparePart(SparePart part) {
			this.part = part;
		}

		public void updateRepairLine(SparePart parts, int amount) {
			this.part = parts;
			this.amount = amount;
		}

		public String toString() {
			return "Part " + part.getIDnumber() + " - "+ amount + " units";
		}

	}
}
